<?php
/*
 * Created on 18 mars 2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

	include("Commons.php");

	$profileUser = param('user');
	
	$sql = "SELECT	u.id,u.username,u.showfriends,c.name AS 'country',p.country as 'cid',p.city, p.gender,p.description,YEAR(CURDATE()) - YEAR(p.birthday) - IF( RIGHT(CURDATE(),5) < RIGHT(p.birthday,5) , 1, 0 ) AS 'age',u.since, ph.id, ph.type
			FROM	user u
			JOIN	profile p ON p.user=u.id AND p.current=1
			JOIN	region c ON c.id=p.country
			LEFT JOIN	photo ph ON ph.id=p.photo
			WHERE	u.username='$profileUser'
			AND		u.banned=0;";
	
	$rh = query($sql);
				
	if ( mysql_num_rows($rh)==0 )
		error404();

	list($uid,$username,$showfriends,$country,$cid,$city,$gender,$description,$age,$since,$photo,$photoType) = mysql_fetch_array($rh);

	$title = fill( getTerm('profile', 'member_profile'), ucfirst($username) );

	if ( ! array_key_exists('browser', $_SESSION) )
	{
		$_SESSION['browser'] = 'browse';
		$_SESSION['browse_mode'] = 'country';
	}
	
	switch ( $_SESSION['browser'] )
	{
		case 'search':
			//$searchurl = formatUrl( '/search/?'. implode($_SESSION['url'], '&') );
			pushNavigationItem( getTerm('search', 'search_for_a_penpal'), '/search/' );
			pushNavigationItem( getTerm('search', 'search_results'), $searchurl );
			

			
			pushNavigationItem( $title, '/account/profile/' );
			printHeader($title, $title, 'search');
			break;
		
		default:
		case 'browse':
			
			if ( $_SESSION['browse_mode']!='country' )
				pushNavigationItem( getTerm('browse', 'by_name'), formatUrl('/browse/name/') );
			else
			{
				pushNavigationItem( getTerm('browse', 'by_country'), formatUrl('/browse/country/') );
				pushRegionNavigationItem($country);
			}
			
			pushNavigationItem( $title, '/account/profile/' );
			printHeader($title, $title, 'browse');
			break;
	}
	
				print '<script type="text/javascript"><!--
google_ad_client = "pub-2652372001711469";
/* Profile - Bottom */
google_ad_slot = "4748431563";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>' ;

	print '<div id="member-profile" class="box">';

	$rh = query("SELECT ip FROM event WHERE user=$uid ORDER BY date DESC LIMIT 1");
	if ( mysql_num_rows($rh) > 0) {
		$row = mysql_fetch_row($rh);
		$ip = $row[0];
		showmap($ip, $cid);
	}
	if ( !empty($photoType) )
	{
		$photoType = strtolower($photoType);
		print "<div class=\"large-photo\"><a href=\"/Photos/$photo.$photoType\" target=\"_blank\"><img src=\"/Thumbs/$photo-medium.$photoType\" /><div>". fill( getTerm('profile','photo_real_size') , $username ) ."</div></a></div>";
	}
	


	print '<dl>';
	
	print '<dt>Age</dt><dd>'. $age .' year-old</dd>';
	print '<dt>Gender</dt><dd>'. getTerm('profile', $gender) .'</dd>';
	print '<dt>Location</dt><dd>'. $city .', '. getTerm('country', $country) .'</dd>';
	print '<dt>Description</dt><dd>'. $description .'</dd>';
	//print '<dt>Member since</dt><dd>'. formatDate($since) .'</dd>';
	
	print '</dl>';
	
	if ( $showfriends==1 )
	{
		$rh = query('	SELECT	u.username,c.name AS \'country\',YEAR(CURDATE()) - YEAR(p.birthday) - IF( RIGHT(CURDATE(),5) < RIGHT(p.birthday,5) , 1, 0 ) AS \'age\',ph.id , ph.type
						FROM	friends f
						JOIN	user u ON f.friend=u.id
						JOIN	profile p ON p.user=u.id AND p.current=1
						JOIN	region c ON p.country=c.id
						LEFT JOIN	photo ph ON ph.id=p.photo
						WHERE	f.user='. $uid .';');
				
		if ( mysql_num_rows($rh)!=0 )
		{
			$profileurl = formatUrl("/profile/${user[username]}");
			print '<div id="friends">';
			print '<h3>'. getTerm('profile', 'my_friends') .'</h3>';
			
			while ( $row = mysql_fetch_array($rh) )
			{
				print '<div id="profile-list" class="friend">';
			
				if ( !empty($row['type']) )
					// print '<img class="photo friend-photo" src="/Thumbs/'. $row['id'] .'-small.'. strtolower($row['type']) .'" />';
		
				print '<div class="username"><a href="'. $profileurl .'">'. $row['username'] .'</a></div>';
				print '<div class="overview">'. fill( getTerm('profile', 'friend_overview'), getTerm('country', $row['country']) ) .'</div>' ;
				print '</div>';
							
			}
			print '</div>';
		}
	}
	
	print '<div id="actions" class="box"><ul>';
	print '<li class="send-message"><a href="/messaging/write/'. $username .'">'. fill( getTerm('profile', 'send_message'), $username) .'</a></li>';
	print '<li class="add-friends"><a href="/friends/add/'. $username .'">'. getTerm('profile', 'add_to_friends') .'</a></li>';
	print '<li class="report-abusive"><a href="/report/'. $username .'">'. getTerm('profile', 'report_abuse') .'</a></li>';
	print '</ul></div>';
	
	print '</div>';
	
	print '<br>';
	
	print '<script type="text/javascript"><!--
google_ad_client = "pub-2652372001711469";
/* Profile - Bottom */
google_ad_slot = "4748431563";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>' ;
	
	
	printFooter();

function showmap($ip, $cid) {
include('./geoip/geo.inc');
include('checks.php');
	list($country, $lat, $long) = ip_resolve($ip);
	$add = '';
	if ($country) {
		$add = (country_id_to_code($cid) == $country) ? '' : '.bindPopup("Not confirmed").openPopup()';
	}
if ($lat && $long) {
print <<<EOL
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<div class="clearfix"><div id="map" class="profRightMap"  style="width: 400px; height: 300px"></div></div>  
<script type="text/javascript">
    var map = L.map('map', {
        zoomControl: true,
        maxZoom: 7,
        minZoom: 0,
        attributionControl:false,
    }).setView([$lat,$long], 3);
    L.tileLayer('http://ipstatic.net/images/maps/{z}/{x}/{y}.png').addTo(map);  
    var marker = L.marker([$lat,$long]).addTo(map)$add;

</script> 
EOL;
}
}

?>
